package com.cyandpos.pad.utils.view;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.posdata.CyFood;
import com.cyandpos.pad.utils.ToastUtils;

import java.util.List;

public class SelectFlavorDialog extends AlertDialog {
    private static  SelectFlavorDialog selectFlavorDialog;
    private Context context;
    private View view;
    private TextView tv_title;
    private RelativeLayout rl_close;
    private GridView gv_title;
    private Button bt_item;
    private Button bt_submit;
    private EditText et_remarks;
    private SelectDialogOnClickListener selectDialogOnClickListener;
    private  TitleAdapter titleAdapter;
    private int pos=-1;
    private String flaType;
    private  SelectFlavorDialog(Context context, SelectDialogOnClickListener selectDialogOnClickListener) {
        super(context);
        this.context=context;
        this.selectDialogOnClickListener=selectDialogOnClickListener;
        view = LayoutInflater.from(context).inflate(R.layout.dialog_select_flavor,null);
        tv_title=view.findViewById(R.id.tv_title);
        rl_close=view.findViewById(R.id.rl_close);
        gv_title=view.findViewById(R.id.gv_pay_more);
        bt_item=view.findViewById(R.id.bt_item);
        bt_submit=view.findViewById(R.id.bt_submit);
        et_remarks=view.findViewById(R.id.et_remarks);
    }

    public static  SelectFlavorDialog getSelectFlavorDialog(Context context, SelectDialogOnClickListener selectDialogOnClickListener){
        if(selectFlavorDialog==null){
            selectFlavorDialog=new SelectFlavorDialog(context,selectDialogOnClickListener);
        }
        return selectFlavorDialog;
    }

    /**
     * 显示弹窗
     * @param titles
     * @param isClose
     */
    public  void showDialog(final List<CyFood.Flavor> titles, boolean isClose){
        selectFlavorDialog.setCancelable(isClose);
        if(titles!=null&&titles.size()>0) {
             titleAdapter = new TitleAdapter(context, titles);
            gv_title.setAdapter(titleAdapter);
        }
        gv_title.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                pos=position;
                flaType=titles.get(position).getSpid()+"";
               if(titleAdapter!=null){
                   titleAdapter.setIndex(position);

               }
            }
        });
        rl_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectFlavorDialog.dismiss();
                selectFlavorDialog=null;
            }
        });
        bt_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectFlavorDialog.dismiss();
                selectFlavorDialog=null;
            }
        });
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(flaType!=null&&!"".equals(flaType)) {
                    if (selectDialogOnClickListener != null) {
                        selectDialogOnClickListener.selectDialogOnClick(flaType, et_remarks.getText() + "", pos);
                    }
                    selectFlavorDialog.dismiss();
                    selectFlavorDialog=null;
                }else{
                    ToastUtils.show("请选择口味!");
                }
            }
        });
        selectFlavorDialog.setView(view,0,0,0,0);
        selectFlavorDialog.show();
        selectFlavorDialog.getWindow().setBackgroundDrawableResource(R.drawable.dialog_bg_common);
    }

    /**
     * 设置标题
     * @param title
     */
    public void setTitle(String title){
        if(tv_title!=null)
            tv_title.setText(title);
    }
    public class TitleAdapter extends BaseAdapter {
        private Context context;
        private List<CyFood.Flavor> contents;
        private int index=-1;
        public TitleAdapter(Context mContext, List<CyFood.Flavor> contents) {
            this.context=mContext;
            this.contents=contents;
        }

        @Override
        public int getCount() {
            return contents.size();
        }

        @Override
        public CyFood.Flavor getItem(int position) {
            return contents.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TitleAdapter.Holder holder;
            if (convertView == null){
                convertView = LayoutInflater.from(context).inflate(R.layout.item_dialog_flavor, parent, false);
                holder=new TitleAdapter.Holder();
                holder.tv_item=convertView.findViewById(R.id.tv_item);
                convertView.setTag(holder);
            }else{
                holder= (Holder) convertView.getTag();
            }
            if(index==position){
                holder.tv_item.setBackgroundResource(R.drawable.food_type_bg_true);
                holder.tv_item.setTextColor(context.getResources().getColor(R.color.white));
            }else{
                holder.tv_item.setBackgroundResource(R.drawable.dialog_flavor_item_false);
                holder.tv_item.setTextColor(context.getResources().getColor(R.color.color_333));
            }
            CyFood.Flavor flavor=getItem(position);
            if(flavor!=null) {
                holder.tv_item.setText(flavor.getName());
            }
            return convertView;
        }
        class  Holder{
            private TextView tv_item;
        }

        public void setIndex(int index) {
            this.index = index;
            notifyDataSetChanged();
        }
    }

    public interface SelectDialogOnClickListener{
        void selectDialogOnClick(String flavor,String remarks,int position);
    }
}

